﻿//功能描述：系统设置-公告管理
using Dapper;
using Hong.Common.Encryption;
using Hong.IRepository.IRepository_SystemSettings;
using Hong.Model.Dto.PesponseDto.ResponseModel.ResultModel;
using Hong.Model.SysDomain.Model_SystemSettings;
using Hong.Model.SysDomain.Model_SystemSettings.ViewModel;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hong.Repository.Repository.Repository_SystemSettings
{
    public class NoticeDal : INotice
    {
        #region 构造器注入

        private readonly DapperHelper _dapperHelper;
        private readonly SecurityStatic _securityStatic;

        public NoticeDal(DapperHelper dapperHelper, SecurityStatic securityStatic)
        {
            _dapperHelper = dapperHelper;
            _securityStatic = securityStatic;
        }
        #endregion


        #region 添加公告
        public ResponseModel<bool> AddNper(NnperModel nnper)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                ResponseModel<bool> res = new ResponseModel<bool>();
                conn.Open();
                using (var begin = conn.BeginTransaction())
                {
                    try
                    {
                        //添加公告
                        conn.Execute($"insert into Notice values('{nnper.notice.Ntitel}','{nnper.notice.Ntxt}','{nnper.notice.Ntime}')");


                        foreach (var item in nnper.nPers.PersonnelIdb)
                        {
                            //添加公告连接人员
                            conn.Execute($"insert into  N_Per values('{nnper.notice.Nid}','{nnper.nPers.PersonnelIda}','{item}')");
                        }
                        begin.Commit();

                        return res.Succeed(true);
                    }
                    catch (Exception)
                    {
                        begin.Rollback();
                        throw;
                    }


                }


            }
        }
        #endregion

        #region 公告显示
        public  PageResponse<NPmodel> GetPer( string PersonnelNamea, int pageindex, int pagesize)
        {
            var sql = "select * from show_Notice_Per where 1=1";

            if (PersonnelNamea != null)
            {
                sql += $" and PersonnelNamea like '%{PersonnelNamea}%'";
            }
            var list =  _dapperHelper.Query<NPmodel>(sql);
            var rlist = list.OrderByDescending(m => m.Nid).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
            PageResponse<NPmodel> q =  new PageResponse<NPmodel>(rlist, pageindex, list.Count());
            return q;
        }

        #endregion

        #region 首页显示公告
        public List<rrrr> getRM()
        {
            
            var list = new List<rrrr>();
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                list = conn.Query<rrrr>($"select Ntitel from show_Notice_Per").ToList();
            }
            return list;
        }
        #endregion

        #region 按部门搜索员工
        public ResponseModel<List<PersonnelModel>> selecPer(int depId)
        {
            ResponseModel<List<PersonnelModel>> res = new ResponseModel<List<PersonnelModel>>();
            var sql = $"select PersonnelId, PersonnelName from Personnel where DepartmentId={depId}";
            var list = _dapperHelper.Query<PersonnelModel>(sql).ToList();
            
            return res.Succeed(list);
        }

        #endregion


    }
}
